View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   //CabaWeb
3   //Copyright (C) 2004  Thomas Vogt <Thomas.Vogt@TVC-Software.com>
4   //
5   //This library is free software; you can redistribute it and/or
6   //modify it under the terms of the GNU Lesser General Public
7   //License as published by the Free Software Foundation; either
8   //version 2.1 of the License, or (at your option) any later version.
9   //
10  //This library is distributed in the hope that it will be useful,
11  //but WITHOUT ANY WARRANTY; without even the implied warranty of
12  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  //Lesser General Public License for more details.
14  //
15  //You should have received a copy of the GNU Lesser General Public
16  //License along with this library; if not, write to the Free Software
17  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  package org.fhw.cabaweb.webfrontend.filters;
20  
21  import java.io.IOException;
22  
23  import javax.servlet.Filter;
24  import javax.servlet.FilterChain;
25  import javax.servlet.FilterConfig;
26  import javax.servlet.ServletException;
27  import javax.servlet.ServletRequest;
28  import javax.servlet.ServletResponse;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  /***
36   * <strong>Filter</strong>-Klasse die im Falle das der Client das unterst&uuml;tzt,
37   * den Stream GZIP komprimiert.
38   *
39   * ORIGINAL aus dem Buch Servlets and JavaServer Pages
40   * von Jayson Falkner, Kevin Jones (http://www.jspbook.com/)
41   *
42   * @author Jayson Falkner, Kevin Jones
43   *
44   * @version Version 1.0 24.07.2004
45   */
46  public class GZIPFilter implements Filter
47  {
48      /***
49       * The <code>Log</code> instance for this application.
50       */
51      private Log log = LogFactory.getLog("org.fhw.cabaweb.webfrontend.filters");
52  
53      public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
54      {
55          if (req instanceof HttpServletRequest)
56          {
57              HttpServletRequest request = (HttpServletRequest) req;
58              HttpServletResponse response = (HttpServletResponse) res;
59              String ae = request.getHeader("accept-encoding");
60  
61              if (ae != null && ae.indexOf("gzip") != -1)
62              {
63                  if (log.isDebugEnabled())
64                  {
65                      log.debug(" GZIP supported, compressing.");
66                  }
67  
68                  GZIPResponseWrapper wrappedResponse = new GZIPResponseWrapper(response);
69                  chain.doFilter(req, wrappedResponse);
70                  wrappedResponse.finishResponse();
71  
72                  return;
73              }
74  
75              chain.doFilter(req, res);
76          }
77      }
78  
79      public void init(FilterConfig filterConfig)
80      {
81          // noop
82      }
83  
84      public void destroy()
85      {
86          // noop
87      }
88  }